/* CookieServer_.java
 *
 * Author:	Malcolm Staudacher
 * Date:	Oct 22, 2008
 *
 */

package corbacookie.Cookie;

import java.rmi.Remote;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.rmi.CORBA.Stub;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

public class CookieServer_ {

	static final String CONTEXT_NAME = "java.naming.factory.initial";
	static final String IIOP_STRING = "com.sun.jndi.cosnaming.CNCtxFactory";
	static final String URL_NAME = "java.naming.provider.url";
	static final String IIOP_URL_STRING = "iiop://localhost:2050";


	public static void main(String args[]) {
		 try{
		 // create and initialize the ORB
		 ORB orb = ORB.init(args, null);
		
		 // get reference to rootpoa & activate the POAManager
		 POA rootpoa =
		 POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
		 rootpoa.the_POAManager().activate();
		
		 // create servant and register it with the ORB
		 CookieImpl ci = new CookieImpl();
		 //PortableRemoteObject.exportObject(ci);
		 //PortableRemoteObject.toStub(ci);
			      
		 ci.setORB(orb);
			      
		
		 // get object reference from the servant
		 org.omg.CORBA.Object ref = rootpoa.servant_to_reference(ci);
		 cookieserver href = cookieserverHelper.narrow(ref);
				  
		 // get the root naming context
		 org.omg.CORBA.Object objRef =
		 orb.resolve_initial_references("NameService");
		 // Use NamingContextExt which is part of the Interoperable
		 // Naming Service (INS) specification.
		 NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
		
		 // bind the Object Reference in Naming
		 String name = "CookieServer";
		 NameComponent path[] = ncRef.to_name( name );
		 ncRef.rebind(path, href);
		
		 System.out.println("CookieServer ready and waiting ...");
		
		 // wait for invocations from clients
		 orb.run();
		 }
				
		 catch (Exception e) {
		 System.err.println("ERROR: " + e);
		 e.printStackTrace(System.out);
		 }
				  
		 System.out.println("HelloServer Exiting ...");

//		try {
//
//			// Create the Object
//			CookieImpl ci = new CookieImpl();
//
//			// Create the IIOP Initial Context
//			Properties iiopProperties = new Properties();
//			iiopProperties.put(CONTEXT_NAME, IIOP_STRING);
//			iiopProperties.put(URL_NAME, IIOP_URL_STRING);
//			InitialContext iiopContext = new InitialContext(iiopProperties);
//
//			// Bind the object to the IIOP registry
//			iiopContext.rebind("CookieServer", ci);
//
//			System.out.println("CookieServer bound in the IIOP Registry");
//		} catch (Exception exception) {
//			exception.printStackTrace();
//		}

	}
}
